package com.feonix.common.util.mvc;

import java.util.HashMap;
import java.util.Map;

import com.feonix.common.util.SpringUtil;
import com.feonix.common.util.StringUtil;

public class JsonResult {
	/**
	 * 是否成功，默认为false
	 */
	private boolean success = false;

	/**
	 * 提示信息，自动获取国际化文件对应的提示
	 */
	private String message = null;

	/**
	 * 状态码
	 */
	private String code = null;

	/**
	 * 自定义json参数
	 */
	private Map<String, Object> params = new HashMap<String, Object>();

	public static JsonResult getInstance() {
		return new JsonResult();
	}

	public boolean isSuccess() {
		return this.success;
	}

	public JsonResult setSuccess(boolean success) {
		this.success = success;
		return this;
	}

	public String getMessage() {
		return this.message;
	}

	public JsonResult setMessage(String message) {
		this.message = SpringUtil.getMessage(message);
		return this;
	}

	@Deprecated
	public JsonResult successAdd() {
		this.message = SpringUtil.getMessage("add.success");
		this.success = true;
		return this;
	}

	@Deprecated
	public JsonResult failAdd() {
		this.message = SpringUtil.getMessage("add.fail");
		this.success = false;
		return this;
	}

	@Deprecated
	public JsonResult successModify() {
		this.message = SpringUtil.getMessage("modify.success");
		this.success = true;
		return this;
	}

	@Deprecated
	public JsonResult failModify() {
		this.message = SpringUtil.getMessage("modify.fail");
		this.success = false;
		return this;
	}

	@Deprecated
	public JsonResult successRemove() {
		this.message = SpringUtil.getMessage("remove.success");
		this.success = true;
		return this;
	}

	@Deprecated
	public JsonResult failRemove() {
		this.message = SpringUtil.getMessage("remove.fail");
		this.success = false;
		return this;
	}

	@Deprecated
	public JsonResult successOperation() {
		this.message = SpringUtil.getMessage("opr.success");
		this.success = true;
		return this;
	}

	@Deprecated
	public JsonResult successOperation(String paramString) {
		this.message = SpringUtil.getMessage(paramString);
		this.success = true;
		return this;
	}

	@Deprecated
	public JsonResult failOperation() {
		this.message = SpringUtil.getMessage("opr.fail");
		this.success = false;
		return this;
	}

	@Deprecated
	public JsonResult failOperation(String messageKey) {
		this.message = SpringUtil.getMessage("opr.fail");
		this.success = false;
		return this;
	}

	public JsonResult set(ResultState resultState) {
		return set(resultState, null);
	}

	public JsonResult set(ResultState resultState, String messageKey) {
		switch (resultState.ordinal() + 1) {
		case 1:
			this.success = true;
			this.message = SpringUtil.getMessage("add.success");
			break;
		case 2:
			this.success = false;
			this.message = SpringUtil.getMessage("add.fail");
			break;
		case 3:
			this.success = true;
			this.message = SpringUtil.getMessage("modify.success");
			break;
		case 4:
			this.success = false;
			this.message = SpringUtil.getMessage("modify.fail");
			break;
		case 5:
			this.success = true;
			this.message = SpringUtil.getMessage("remove.success");
			break;
		case 6:
			this.success = false;
			this.message = SpringUtil.getMessage("remove.fail");
			break;
		case 7:
			this.success = true;
			this.message = SpringUtil.getMessage("opr.success");
			break;
		case 8:
			this.success = false;
			this.message = SpringUtil.getMessage("opr.fail");
		}

		if (StringUtil.isNotEmpty(messageKey)) {
			this.message = SpringUtil.getMessage(messageKey);
		}
		return this;
	}

	public String getCode() {
		return this.code;
	}

	public JsonResult setCode(String code) {
		this.code = code;
		return this;
	}

	public Map<String, Object> getParams() {
		return this.params;
	}

	public JsonResult addParam(String key, Object value) {
		if (StringUtil.isNotEmpty(key)) {
			this.params.put(key, value);
		}
		return this;
	}
}
